package de.dlrg.wms.controller.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.dlrg.wms.controller.interfaces.ICRUD;
import de.dlrg.wms.dao.AgeClassDao;
import de.dlrg.wms.dao.SwimmerDao;
import de.dlrg.wms.model.AgeClass;
import de.dlrg.wms.model.Swimmer;
import de.dlrg.wms.model.transformed.AgeClassToSwimmer;
@Component
public class AgeClassController implements ICRUD<AgeClass> {
	@Autowired
	private AgeClassDao ageClassDao;
	@Autowired
	private DisciplineController disciplineController;
	@Autowired
	private SwimmerDao swimmerDao;
	
	public DisciplineController getDisciplineController() {
		return this.disciplineController;
	}
	
	public List<AgeClass> readAll() {
		return this.ageClassDao.readAll();
	}

	public void createOrUpdate(AgeClass toCreateOrUpdate) {
		this.ageClassDao.save(toCreateOrUpdate);
	}

	public void delete(AgeClass toDelete) {
		this.ageClassDao.delete(toDelete);
	}

	public void validate(AgeClass toValidate) {
		// TODO Auto-generated method stub
		
	}

	public List<AgeClassToSwimmer> readAllAgeClassToSwimmer() {
		List<AgeClassToSwimmer> ageClassToSwimmers = new ArrayList<AgeClassToSwimmer>();
		for(AgeClass ageClass : this.readAll()) {
			List<Swimmer> swimmerOfAgeClass = this.readAgeClassToSwimmer(ageClass);
			AgeClassToSwimmer newAgeClassToSwimmer = new AgeClassToSwimmer();
			newAgeClassToSwimmer.getSwimmers().addAll(swimmerOfAgeClass);
			newAgeClassToSwimmer.setAgeClass(ageClass);
			
			ageClassToSwimmers.add(newAgeClassToSwimmer);
		}
		return ageClassToSwimmers;
	}
	
	public List<Swimmer> readAgeClassToSwimmer(AgeClass ageClass) {
		return this.swimmerDao.findByAgeClass(ageClass);
	}
}
